using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using AtletIS.DomainModel.Entities.Main;

namespace AtletIS.Database.Mapping.Main
{
    public class ChipDataMap : EntityTypeConfiguration<ChipData>
    {
        public ChipDataMap()
        {
            // Primary Key
            this.HasKey(t => new { t.CompetitionId, t.MeasurementId, t.PcId, t.DataIndex });

            // Properties
            this.Property(t => t.CompetitionId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.MeasurementId)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.PcId)
                .IsRequired()
                .HasMaxLength(10);

            this.Property(t => t.DataIndex)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.ChipTag)
                .HasMaxLength(10);

            // Table & Column Mappings
            this.ToTable("ChipData");
            this.Property(t => t.CompetitionId).HasColumnName("CompetitionId");
            this.Property(t => t.MeasurementId).HasColumnName("MeasurementId");
            this.Property(t => t.PcId).HasColumnName("PcId");
            this.Property(t => t.DataIndex).HasColumnName("DataIndex");
            this.Property(t => t.ChipTag).HasColumnName("ChipTag");
            this.Property(t => t.ChipTagsGroupId).HasColumnName("ChipTagsGroupId");
            this.Property(t => t.LapCount).HasColumnName("LapCount");
            this.Property(t => t.ChipDateTime).HasColumnName("ChipDateTime");

            // Relationships
            this.HasRequired(t => t.Competition)
                .WithMany(t => t.ChipData)
                .HasForeignKey(d => d.CompetitionId);
            this.HasOptional(t => t.ChipTag1)
                .WithMany()
                // .WithMany(t => t.ChipDatas)
                .HasForeignKey(d => new { d.ChipTagsGroupId, d.ChipTag });

        }
    }
}
